<?php
defined('EXEC') or die();

//Verificando recuperar senha
if (@$_POST['nm_login'] && @$_POST['ds_email'] && @$_POST['nm_cpf']) {
	$auth->passRecovery();
}

//Autenticando o usuário e o redirecionando
if(@$_POST['user'] && @$_POST['pass']){
	$test = $auth->authenticate();
}

?>


    <?php include_once('top.php'); ?>
	
    <div class="container">
	    <div class="col-sm-4 col-md-offset-4">
		    <div class="panel panel-default">
			    <div class="panel-heading"><i class="fa fa-lock fa-lg"></i> Área Restrita</div>
			 	<div class="panel-body">
				<form class="form-horizontal" method="post" role="form" onsubmit="return testLogin();" id="formLogin">
					<div class="form-group">
						<label class="col-sm-2 control-label">Login:</label>
						<div class="col-sm-10">
							<input name="user" id="user" type="text" maxlength="200" value="<?php echo @$_POST['user']; ?>" class="form-control" placeholder="Digite seu login">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label">Senha:</label>
						<div class="col-sm-10">
						<input name="pass" id="pass" type="password" maxlength="200" class="form-control" placeholder="Digite sua senha">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label">Modulo:</label>
						<div class="col-sm-10">
							<select id="modulo" name="modulo" class="form-control">
								<?php
								$rs = query("select ci_modulo, ds_modulo from tethys.tb_modulo");
								while ($row = $rs->fetch()){
									echo "<option value='".$row['ci_modulo']."'>".$row['ds_modulo']."</option>";
								}
								?>
							</select>
						</div>
					</div>
					<div class="form-group">
						<div class="col-sm-2">
						</div>
						<div class="col-sm-10">
							<input class="btn btn-default" id="btLogin" type="submit" value="Enviar" style="width:80px;"/>
							<input type="hidden" name="url" value="<?php echo $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; ?>"/>
						</div>
					</div>
					<div class="form-group">
						<div class="col-sm-2">
						</div>
						<div class="col-sm-10"> 
							<a href="" data-toggle="modal" data-target="#myModal"><i class="fa fa-info-circle"></i> Esqueceu a senha?</a>
						</div>
					</div>
				</form>
			</div>
		</div>
		</div>
    </div>
    
    <!-- Modal -->
	<div class="modal fade" id="myModal" tabindex="0" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog">
	    <div class="modal-content">
	      <div class="modal-header">
	        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
	        <h4 class="modal-title text-success">Recuperação de senha</h4>
	      </div>
	      <div class="modal-body">
			<form class="form-horizontal" role="form" method="post" onsubmit="return test();" id="formRecupSenha">
                <div id="alertModal"></div>
				<div class="form-group">
					<label for="inputEmail3" class="col-sm-4 control-label">Login:</label>
					<div class="col-sm-5">
						<input name="nm_login" id="nm_login" type="text" placeholder="Digite seu usuário" size="35" maxlength="200" class="form-control" style="padding:4px;">
					</div>
				</div>
				<div class="form-group">
					<label for="inputEmail3" class="col-sm-4 control-label">E-mail:</label>
					<div class="col-sm-5">
						<input name="ds_email" id="ds_email" placeholder="Informe seu e-mail" type="text" size="35" maxlength="200" class="form-control" style="padding:4px;">
					</div>
				</div>
				<div class="form-group">
					<label for="inputEmail3" class="col-sm-4 control-label">CPF:</label>
					<div class="col-sm-5">
						<input name="nm_cpf" id="nm_cpf" placeholder="Informe seu CPF" type="text" onkeypress="mask(this, cpf)" size="20" maxlength="14" class="form-control" style="padding:4px;">
					</div>
				</div>
			
	      </div>
	      <div class="modal-footer">
	      	<button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
	        <button type="submit" class="btn btn-success" id="btn-enviar">Enviar</button></form>
	      </div>
	    </div><!-- /.modal-content -->
	  </div><!-- /.modal-dialog -->
	</div><!-- /.modal -->	
	
    
<?php include_once('footer.php'); ?>

<script type="text/javascript">
    function updateModalTips(obj, t, timer){
        title = "";
        text = t;
        type = "danger";
        icon = "exclamation-triangle";
        if(timer == undefined) timer = 3500;
        if(title =='') title = "Alerta:";
        var template = $('<div class="alert alert-'+type+'" role="alert"><button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button><i class="fa fa-'+icon+' fa-lg"></i> <strong>'+title+'</strong><br/> '+text+'</div>');
        $("#alertModal").prepend(template);
        template.hide().slideDown();

        if(timer!=null){
            console.log(timer);
            setTimeout(function(){
                template.slideUp('slow');
            }, timer);
        }
        obj.focus().parent().addClass("has-error");
        $(document).find("html, body").animate({ scrollTop: 0 }, "slow");
    }

    function test(){
        var valid = true;
        var login = $('#nm_login').val();
        var email = $('#ds_email').val();
        var cpf = $('#nm_cpf').val();

        $("#formRecupSenha").find("input,select,textarea").each(function(index){
            $(this).parent().removeClass("has-error");
        });

        if(!login){
            updateModalTips($('#nm_login'), 'Informe o login do usuario.');
            valid = false;
        }else if(!email){
            updateModalTips($('#ds_email'), 'Informe o email do usuario.');
            valid = false;
        }else if(!checkMail(email)){
            updateModalTips($("#ds_mail"), 'Email inválido. Digite corretamente o email!');
            valid = false;
        }else if(!cpf){
            updateModalTips($('#nm_cpf'), 'Informe o CPF do usuario.');
            valid = false;
        }else if(!checkCpf(cpf)){
            updateModalTips($('#nm_cpf'), 'CPF inválido. Digite corretamente o CPF!');
            valid = false;
        }

        return valid;
    }

    function testLogin(){
        var valid = true;
        $("#formLogin").find("input,select,textarea").each(function(index){
            $(this).parent().removeClass("has-error");
        });

        if($('#user').val() == 0 || $('#user').val() == "" || $('#user').val() == null ){
            updateTips($('#nm_login'), 'Informe um usuário.');
            valid = false;
        }else if($('#pass').val() == 0 || $('#pass').val() == "" || $('#pass').val() == null ){
            updateTips($('#nm_login'), 'Senha inválida.');
            valid = false;
        }

        return valid;
    }
</script>
